package Practice4_Regex;

import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Template {
    final String template;
    final Pattern pattern = Pattern.compile("\\$\\{(\\w+)\\}");

    public Template(String template) {
        this.template = template;
    }

    public String render(Map<String, Object> data) {
        Matcher m = pattern.matcher(template);
        // TODO:
        StringBuilder sb = new StringBuilder();
        while (m.find()) {
            m.appendReplacement(sb, (String) data.get(m.group(1)));
        }
        m.appendTail(sb);
        return sb.toString();
    }
}
